﻿using ConsoleHoster.Common.Model;
using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.IO;
using ConsoleHoster.Model;
using ConsoleHoster.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleHoster.UnitTests.ModelTests
{
	[TestClass]
	public class StorageManagerTests
	{
		private Mock<IFileSystemContentProvider> mockFileSystemContentProvider;
		private Mock<IXmlStorage> mockXmlStorage;
		private Mock<ILogger> mockLogger;

		private StorageManager sut;

		[TestInitialize]
		public void Initialize()
		{
			this.mockFileSystemContentProvider = new Mock<IFileSystemContentProvider>();
			this.mockXmlStorage = new Mock<IXmlStorage>();
			this.mockLogger = new Mock<ILogger>();

			this.sut = new StorageManager(this.FileSystemContentProvider, this.XmlStorage, this.Logger);
		}

		[TestMethod]
		public void LoadGlobalSettings_LoadsGlobalCommands()
		{
			const string tmpXml = @"<?xml version='1.0' encoding='utf-8'?>
<HosterConfig>
  <Projects />
  <GenericCommands>
    <Command Name='dir' Group='Navigation' CommandText='dir' isFinal='true' />
  </GenericCommands>
</HosterConfig>";

			this.mockFileSystemContentProvider
				.Setup(fs => fs.FileExists(Constants.ProjectsFilename))
				.Returns(true);

			this.mockXmlStorage
				.Setup(xs => xs.LoadXDocument(Constants.ProjectsFilename))
				.Returns(XDocument.Parse(tmpXml));
			FontSettings tmpFontSettings;
			IEnumerable<CommandData> tmpCommands;
			this.sut.LoadGlobalSettings(out tmpFontSettings, out tmpCommands);

			Assert.IsNotNull(tmpCommands);
			Assert.AreEqual(1, tmpCommands.Count());

			CommandData tmpDirCommand = tmpCommands.First();
			Assert.AreEqual("dir", tmpDirCommand.Name);
			Assert.AreEqual("Navigation", tmpDirCommand.GroupName);
			Assert.AreEqual(true, tmpDirCommand.IsFinal);
		}

		#region Properties
		private IFileSystemContentProvider FileSystemContentProvider
		{
			get
			{
				return this.mockFileSystemContentProvider.Object;
			}
		}

		private IXmlStorage XmlStorage
		{
			get
			{
				return this.mockXmlStorage.Object;
			}
		}

		private ILogger Logger
		{
			get
			{
				return this.mockLogger.Object;
			}
		}
		#endregion
	}
}
